﻿using BaifudataPay.Applib;
using Com.Allscore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BaifudataPay
{
    public partial class BankPayApi : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string idefault_bank = RequestUtil.FormString("default_bank");
            decimal iamount = RequestUtil.FormDecimal("amount");
            string isubject = RequestUtil.FormString("subject");
            string sign = RequestUtil.FormString("sign");
            string out_trade_no = RequestUtil.FormString("out_trade_no");
            //string signstr = MD5Util.GetShortMD5(string.Format("{0}amount={1}", AppInfo.EncryptKey, order_amount));
            if (idefault_bank.Length < 1 || iamount <= 0 || sign.Length < 1 || out_trade_no.Length < 1)
            {
                Response.Write("非法请求!");
                Response.End();
            }
            // 纯网银支付数据提交接口
            string host_ip = "http://baifupay.34537.com/";


            //必填参数//
            //请与贵网站订单系统中的唯一订单号匹配

            string service = "directPay";

            string outOrderId = out_trade_no;

            string outAcctId = ""; // 外部帐户ID
            string transAmt = iamount.ToString();

            string subject = isubject;
            string body = "订单内容";
            string payMethod = "bankPay"; // 纯网银支付方式
            string certType = "debit"; // 储蓄卡
            string notifyUrl = host_ip + "notifyRSA.aspx";
            string returnUrl = host_ip + "returnRSA.aspx";
            string detailUrl = host_ip + "detail.aspx";
            string channel = "B2C"; // 网银加密方式
            string signType = "RSA"; // 数据加密方式
            string merchantId = "001017041102320";
            string defaultBank = idefault_bank;
            string cardAttr = "01"; // 卡类标识, 储蓄卡


            ////////////////////////////////////////////////////////////////////////////////////////////////

            //把请求参数打包成数组
            SortedDictionary<string, string> sParaTemp = new SortedDictionary<string, string>();

            /*
            sParaTemp.Add("outOrderId", outOrderId);  //外部交易号
            sParaTemp.Add("outAcctId", outAcctId);    //外部账户ID

            sParaTemp.Add("signType", signType);   //签名类型

            */
            //////


            sParaTemp.Add("service", service);
            sParaTemp.Add("merchantId", merchantId);
            //sParaTemp.put("terminalId", terminalId);
            //sParaTemp.put("outOrderId", outOrderId);
            sParaTemp.Add("transAmt", transAmt);


            sParaTemp.Add("outOrderId", outOrderId);


            sParaTemp.Add("subject", subject);
            sParaTemp.Add("body", body);
            //  		sParaTemp.put("name", name);
            //  		sParaTemp.put("id", certId);
            //  		sParaTemp.put("mobile", mobile);
            sParaTemp.Add("signType", signType);
            sParaTemp.Add("payMethod", payMethod);
            if (payMethod == "fastPay")
            {
                sParaTemp.Add("cardType", certType);
                sParaTemp.Add("outAcctId", outAcctId);
            }

            if (payMethod == "bankPay")
            {
                sParaTemp.Add("defaultBank", defaultBank);
                sParaTemp.Add("channel", channel);
                sParaTemp.Add("cardAttr", cardAttr);

            }
            if (detailUrl != "")
            {
                sParaTemp.Add("detailUrl", detailUrl);
            }
            if (notifyUrl != "")
            {
                sParaTemp.Add("notifyUrl", notifyUrl);
            }
            if (returnUrl != "")
            {
                sParaTemp.Add("returnUrl", returnUrl);
            }

            LogUtil.Instance.WriteInfo(JsonUtil.stringify(sParaTemp)); // 记录日志

            ////

            Service allscore = new Service();
            string sHtmlText = "";

            /*
                    sParaTemp.Add("transAmt", transAmt);   //交易金额
                    sParaTemp.Add("subject", out_subject.Value.Trim());   //
                    sParaTemp.Add("body", out_body.Value.Trim());   //
            */
            sHtmlText = allscore.allscorePay(sParaTemp);


            Response.Write(sHtmlText);
        }
    }
}